
# install and load packages
if(!require(tidyverse)){
  install.packages("tidyverse")
  library(tidyverse)
}

if(!require(cowplot)){
  install.packages("cowplot")
  library(cowplot)
}

if(!require(lubridate)){
  install.packages("lubridate")
  library(lubridate)
}

# load datasets created by
# civilsociety_preparation.R
load("data/civilsociety.RData")

# freedom from control : Authoritarian Regimes
a1 <- ggplot(data = authoritarian_regime, mapping = aes(x = year, y = v2cseeorgs))
a1 <- a1 + geom_line(color = alpha("grey", 0.7), aes(group = iso3c)) + 
      geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
      background_grid(major = "xy", minor = "none") +     
      labs(x = "",
           y = "Freedom of Government Constraints",
           title = "G20 Authoritarian Regimes, 2008-2018") + 
       scale_x_date(date_labels = "%Y")

a1

# freedom from control : Hybrid Regimes
a2 <- ggplot(data = hybrid_regime, mapping = aes(x = year, y = v2cseeorgs))
a2 <- a2 + geom_line(color = alpha("grey", 0.7), aes(group = iso3c)) + 
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Freedom of Government Constraints",
       title = "G20 Hybrid Regimes, 2008-2018") +
  scale_x_date(date_labels = "%Y") 

a2

# freedom from control :Flawed Democracies
a3 <- ggplot(data = flawed_democracy, mapping = aes(x = year, y = v2cseeorgs))
a3 <- a3 + geom_line(color = alpha("grey", 0.7), aes(group = iso3c)) + 
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Freedom of Government Constraints",
       title = "G20 Flawed Democracies, 2008-2018") +
  scale_x_date(date_labels = "%Y")

a3

# freedom from control : Full Democracies
a4 <- ggplot(data = full_democracy, mapping = aes(x = year, y = v2cseeorgs))
a4 <- a4 + geom_line(color = alpha("grey", 0.7), aes(group = iso3c)) + 
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Freedom of Government Constraints",
       title = "G20 Full Democracies, 2008-2018") + 
  scale_x_date(date_labels = "%Y")

a4

# combine all freedom from control plots
freedom_from_control <- plot_grid(a1, a2, a3, a4, labels = c("A", "B","C","D"))
freedom_from_control

# save grid plot in a pdf
ggsave(filename="analysis/freedom_from_control.pdf", 
       plot=freedom_from_control,
       width = 25, height = 25,
       units = "cm")

# self-organization:  Authoritarian Regimes
b1 <- ggplot(data = authoritarian_regime, mapping = aes(x = year, y = v2csprtcpt))
b1 <- b1 + geom_line(color="gray70", aes(group = iso3c)) + 
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Participation of Diverse CSOs",
       title = "G20 Authoritarian Regimes, 2008-2018") +
  scale_x_date(date_labels = "%Y") 

b1

# self-organization: Hybrid Regimes
b2 <- ggplot(data = hybrid_regime, mapping = aes(x = year, y = v2csprtcpt))
b2 <- b2 + geom_line(color="gray70", aes(group = iso3c)) + 
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Participation of Diverse CSOs",
       title = "G20 Hybrid Regimes, 2008-2018") +
  scale_x_date(date_labels = "%Y")

b2

# self-organization: Flawed Democracies
b3 <- ggplot(data = flawed_democracy, mapping = aes(x = year, y = v2csprtcpt))
b3 <- b3 + geom_line(color="gray70", aes(group = iso3c)) + 
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Participation of Diverse CSOs",
       title = "G20 Flawed Democracies, 2008-2018") +
  scale_x_date(date_labels = "%Y")

b3

# self-organization: Full Democracies
b4 <- ggplot(data = full_democracy, mapping = aes(x = year, y = v2csprtcpt))
b4 <- b4 + geom_line(color="gray70", aes(group = iso3c)) + 
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Participation of Diverse CSOs",
       title = "G20 Full Democracies, 2008-2018") +
  scale_x_date(date_labels = "%Y")

b4

# combine all self-organization plots
self_organization <- plot_grid(b1, b2, b3, b4, labels = c("A", "B","C","D"))
self_organization

# save grid plot in a pdf
ggsave(filename="analysis/self_organization.pdf", 
       plot=self_organization,
       width = 25, height = 25,
       units = "cm")

# CSO Fredom of Expression: Authoritarian Regime
c1 <- ggplot(data = authoritarian_regime, mapping = aes(x = year, y = v2csreprss)) 
c1 <- c1 + geom_line(color="gray70", aes(group = iso3c)) + guides(colour=FALSE) +
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Freedom of Government Repression",
       title = "G20 Authoritarian Regimes, 2008-2018") +
  scale_x_date(date_labels = "%Y")

c1

# CSO Fredom of Expression: Hybrid Regimes
c2 <- ggplot(data = hybrid_regime, mapping = aes(x = year, y = v2csreprss)) 
c2 <- c2 + geom_line(color="gray70", aes(group = iso3c)) + guides(colour=FALSE) +
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Freedom of Government Repression",
       title = "G20 Hybrid Regimes, 2008-2018") +
  scale_x_date(date_labels = "%Y")

c2

# CSO Fredom of Expression: Flawed Democracy 
c3 <- ggplot(data = flawed_democracy, mapping = aes(x = year, y = v2csreprss)) 
c3 <- c3 + geom_line(color="gray70", aes(group = iso3c)) + guides(colour=FALSE) +
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Freedom of Government Repression",
       title = "G20 Flawed Democracies, 2008-2018") +
  scale_x_date(date_labels = "%Y")

c3

# CSO Fredom of Expression: Full Democracy 
c4 <- ggplot(data = full_democracy, mapping = aes(x = year, y = v2csreprss)) 
c4 <- c4 + geom_line(color="gray70", aes(group = iso3c)) + guides(colour=FALSE) +
  geom_smooth(size = 1.5, method = "loess", se = FALSE, color="red") +
  background_grid(major = "xy", minor = "none") +     
  labs(x = "",
       y = "Freedom of Government Repression",
       title = "G20 Full Democracies, 2008-2018") +
  scale_x_date(date_labels = "%Y")

c4

freedom_of_expression <- plot_grid(c1, c2, c3, c4, labels = c("A", "B","C","D"))
freedom_of_expression

# save grid plot in a pdf
ggsave(filename="analysis/freedom_of_expression.pdf", 
       plot=freedom_of_expression,
       width = 25, height = 25,
       units = "cm")





